﻿<?php
	if($_POST['DO_ALTERACAO']){
		$model = new core\Model('ASSOCIATE','app\libs\Association');
		$a = new app\libs\Association(0,$_REQUEST['CLASSE'],$_REQUEST['TABELA']);
		$ret = $model->create($a);

		if($ret){			
		}		
	}

	if($KEY){
		$model = new core\Model('ASSOCIATE','app\libs\Association');
		$modelAtt = new core\Model('ASSOCIATE_ATT','app\libs\AssociationAtt');

		$a = $model->read(array("ASSOCIATE"=>$KEY));
		$a = $a[0];

		$att = $modelAtt->read(array("ASSOCIATE"=>$KEY));
	}	
?>
	<div class='body' style='padding-left: 15px; width: 90%; margin: auto;'><br/>
		<form method='post' action='/associacao/inclusao'>			
			<fieldset>
				<legend><b>Alterar Associação</b></legend>
				<table class='body' style='width: 100%;'>
					<tr>
						<td><b>Tabela:</b></td>
						<td><span class='k-textbox' style='width: 220px;'><input type='text' name='TABELA' size='60' maxlength='100' value='<?php echo $a->getTable();?>' ></span></td>
						<td><b>Classe:</b></td>
						<td><span class='k-textbox' style='width: 220px;'><input type='text' name='CLASSE' size='60' maxlength='100' value='<?php echo $a->getClass();?>' ></span></td>
						<td style='width: 400px;'>&nbsp;</td>
						<input type='hidden' name='ASSOCIATION_KEY' value='<?php echo $a->getId();?>'  />
					</tr>
					<tr><td colspan='20' width: 100%><hr/></td></tr>
				</table>
				<table>
					<tr>
						<td><b>Coluna</b></td>
						<td><b>Atributo</b></td>
						<td><b>É Objeto</b></td>
					</tr>
<?php
		if($_REQUEST['EDICAO']){
			foreach ($_REQUEST[EDICAO] as $i => $e) {
	        	echo "<input type='hidden' name='EDICAO[$i][ID]' value='{$e['ID']}' />";
	        	echo "<tr>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][COLUNA]' /></span></td>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][ATRIBUTO]' /></span></td>";
	        	echo "<td style='text-align: center'><input type='checkbox' size='40' maxlength='40' name='EDICAO[{$i}][IS_OBJ]' value='S' /></td>"; 
				echo "<input type='hidden' name='EDICAO[$i]['ASSOCIATE_ATT']' value='{$e['ASSOCIACAO_ATT']}' >";
	        	echo "</tr>";
	        } 
	        $i++;      
		}
		if($att){
			foreach ($att as $i => $e) {	        	
	        	echo "<tr>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][COLUNA]' value='{$e->getColumn()}' /></span></td>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][ATRIBUTO]' value='{$e->getAttribute()}' /></span></td>";
	        	echo "<td style='text-align: center'><input type='checkbox' size='40' maxlength='40' name='EDICAO[{$i}][IS_OBJ]' value='S'  /></td>";
				echo "<input type='hidden' name='EDICAO[$i]['ASSOCIATE_ATT']' value='{$e->getId()}' >";
	        	echo "</tr>";
	        } 
	        $i++;
		}
		for($seq = $i+3; $seq > $i; $i++){
			if(!$erro){
				echo "<tr>";
				echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][COLUNA]' /></span></td>";
				echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][ATRIBUTO]' /></span></td>";
				echo "<td style='text-align: center'><input type='checkbox' size='40' maxlength='40' name='EDICAO[{$i}][IS_OBJ]' value='S' /></td>";
				echo "<input type='hidden' name='EDICAO[$i]['ASSOCIATE_ATT']' value='' >";        	
				echo "</tr>";
			}	
		}
?>			
				</table>
				<p><input type='submit' class='k-button' value='Mais' /></p>
				<p><input type='submit' class='k-button' name="DO_ALTERACAO" value='Gravar'></p>
			</fieldset>
		</form>
	</div>